/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rmiclient;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;
import rmiserver.Ticket;
import rmiserver.Trip;

/**
 * Диалог для резервирования билета
 * @author alexxxey
 */
public class ReserveTicketDialog extends JDialog
{    
    /**
     * Создание диалога
     *
     * @param trip - Маршрут, на который резервируется билет
     * @param freePlaces - свободные места на маршрут
     */
    public ReserveTicketDialog(Trip trip, ArrayList<Integer> freePlaces) 
            throws ParseException
    {           
        m_trip = trip;                
        m_freePlaces = new ArrayList<>(freePlaces);
        m_isOK = false;
        
        setTitle("Резервирование билета");  
        setModal(true);
        
        JPanel contentPane = new JPanel();
        setContentPane(contentPane);        
        contentPane.setLayout(new BorderLayout());        
        contentPane.setBorder(new EmptyBorder(5, 10, 5, 5));
        
        contentPane.add(new JLabel(m_trip.getInfo()), BorderLayout.NORTH);
        
        JPanel dataPane = new JPanel();
        dataPane.setLayout(new GridLayout(4, 2, 10, 10));
        dataPane.setBorder(new EmptyBorder(15, 0, 5, 5));
        
        dataPane.add(new JLabel("Имя")); 
        m_firstnameField = new JFormattedTextField();
        
        dataPane.add(m_firstnameField);
        
        dataPane.add(new JLabel("Фамилия")); 
        m_lastnameField = new JFormattedTextField();
        dataPane.add(m_lastnameField);
        
        dataPane.add(new JLabel("№ паспорта"));
                        
        m_pasportField = new JFormattedTextField(new MaskFormatter("######"));
        dataPane.add(m_pasportField);        
                
        JButton choosePlaceBtn = new JButton("Место");
        choosePlaceBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                String[] freePlaces = new String[m_freePlaces.size()];
                for (int i=0; i < freePlaces.length; ++i)
                {
                    freePlaces[i] = m_freePlaces.get(i).toString();
                }
                
                String res = (String)JOptionPane.showInputDialog( 
                        null,
                        "Выберете место",
                        "Выбор места",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        freePlaces,
                        m_freePlaces.get(0).toString());                 
                
                m_placeField.setText(" ");
                m_placeField.setText(res);
            }
        });
        dataPane.add(choosePlaceBtn);        
        m_placeField = new JFormattedTextField();
        m_placeField.setEditable(false);
        dataPane.add(m_placeField);
                        
        contentPane.add(dataPane, BorderLayout.CENTER);
        
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new GridLayout(1, 2, 10, 10));
        buttonPane.setBorder(new EmptyBorder(10,0,5,5));
        JButton okBtn = new JButton("OK");
        okBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                m_isOK = !m_firstnameField.getText().isEmpty() && 
                        !m_lastnameField.getText().isEmpty() &&
                        !m_placeField.getText().isEmpty();                
                //m_isOK = true;
                dispose();
            }
        });
        JButton cancelBtn = new JButton("Отмена");
        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                dispose();
            }
        });
        
        buttonPane.add(okBtn);
        buttonPane.add(cancelBtn);
        contentPane.add(buttonPane, BorderLayout.SOUTH);
        pack();
        
        setVisible(true);                
    }
    
    /**
     * Получение информации о зарезервированном билете
     * 
     * @return Информация о зарезервированном билете 
     */
    public Ticket getTicket()
    {
        return new Ticket(
                m_trip.getTripId(),
                new Integer(m_placeField.getText()),
                new Integer(m_pasportField.getText()),
                m_firstnameField.getText(),
                m_lastnameField.getText());
    }
    
    public boolean isOk()
    {
        return m_isOK;
    }
    
           
    private JFormattedTextField m_firstnameField;
    private JFormattedTextField m_lastnameField;
    private JFormattedTextField m_pasportField;
    private JFormattedTextField m_placeField;      
    
    private Trip m_trip;    
    private ArrayList<Integer> m_freePlaces;
    private boolean m_isOK;
}